<?php

require_once 'init.php';
require_once 'cf/shop.php';

if (!$user) {
	header('Location: /register');
	exit;
}

if(isset($_GET['akcio']) && $_GET['akcio']!='') {
	$smarty->assign('persakcio','true');
}

$customer = cf\Customer::get($user->id());

if (!empty($_POST))
{
	$pass = cf\param::get('pass');
	$name = cf\param::get('username');
	$phone = cf\param::get('phone');
	$address = cf\param::get('address');
	
	if (!$name || (strlen($pass)>0 && strlen($pass)<5) || (strlen($pass)>0 && $pass!=cf\param::get('pass_check'))) {
		exit;
	}
	
	if (strlen($pass)) {
		cf\User::setPassword($user->id(),$pass);
	}
	
	cf\execQuery(
		'UPDATE cf_users SET name=:name WHERE id=:uid',
		array('name'=>$name, 'uid'=>$user->id())
	);
	
	cf\execQuery(
		'UPDATE cf_customers SET phone=:phone WHERE user_id=:uid',
		array('phone'=>$phone, 'uid'=>$user->id())
	);
	
	cf\execQuery(
		'UPDATE cf_customer_addresses SET address=:address WHERE customer_id=:cid',
		array('address'=>$address, 'cid'=>$customer['id'])
	);
	
	$user = cf\User::getLoggedIn();
	$customer = cf\Customer::get($user->id());
}

$name = $login = $email = $phone = $address = '';
$smarty->assignByRef('username',$name);
$smarty->assignByRef('login',$login);
$smarty->assignByRef('phone',$phone);
$smarty->assignByRef('address',$address);

$login = $email = $user->loginName();
$name = $user->name();

if(isset($customer['phone'])) {$phone = $customer['phone'];}
if(isset($customer['address'])) {$address = $customer['address'];}

$smarty->assign('register_error','');

show(
	'personal', 
	'Личный кабинет', 
	array(array('name'=>'Личный кабинет'))
);
